package com.tchintech.pay.view.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.ChangePwDelegate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/14 0014.
 */

public class ChangePwActivity extends ActivityPresenter<ChangePwDelegate> implements View.OnClickListener {
    private Handler handler;
    private String old_pw, new_pw, rep_pw;
    private String change_url = "PassWord.alter";
    private Map<String, String> map;
    private TextView tv_phone;

    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setTitle("修改密码");
        viewDelegate.setVisible();
        tv_phone = viewDelegate.getTv_phone();
        tv_phone.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_phone.getPaint().setAntiAlias(true);//抗锯齿
        viewDelegate.setOnClickListener(this, R.id.change_bt, R.id.tv_phone, R.id.iv_back);
        handler();
    }


    private void urlHttp() {
        map = new HashMap<String, String>();
        map.put("phone", AppConst.getUser().getPhone());
        map.put("old_pw", old_pw);
        map.put("new_pw", new_pw);
        map.put("new_pw", new_pw);
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(this, handler, change_url, map, HttpCode.CHANGE_PASSWORD);
    }


    @Override
    protected void initData() {
        super.initData();
    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.CHANGE_PASSWORD:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                showMsg("密码修改成功");
                                finish();
                            } else {
                                showMsg(json.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;

                }
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.change_bt:
                juge();
                break;
            case R.id.tv_phone:
                Uri uri = Uri.parse("tel:4006500819");
                Intent callIntent = new Intent(Intent.ACTION_CALL, uri);
                startActivity(callIntent);
                break;
            case R.id.iv_back:
                finish();
                break;
        }

    }

    private void juge() {
        old_pw = viewDelegate.getTexts(R.id.edit_old);
        new_pw = viewDelegate.getTexts(R.id.edit_new);
        rep_pw = viewDelegate.getTexts(R.id.edit_rep);
        if (!old_pw.equals("")) {
            if (!new_pw.equals("")) {
                if (new_pw.equals(rep_pw)) {
                    urlHttp();
                } else {
                    showMsg("两次密码输入不一致");
                }
            } else {
                showMsg("请输入新密码");
            }
        } else {
            showMsg("原始密码不能为空");
        }

    }

    @Override
    protected Class<ChangePwDelegate> getDelegateClass() {
        return ChangePwDelegate.class;
    }
}
